// Two function need to be implemented , a2t_handler & a2t_handler_configMode
// used GM Variables
// ds_huntRuleCurrent : current hunt rule object .

function a2t_handler(){
    if(GM_safeVariables["ds_huntRuleCurrent"] != null && GM_safeVariables["ds_huntRuleCurrent"] != ''){
        //second phase, submit the attack action.
        if(window.location.search == ""){
          //encount error , hero couldn't attack that place because out of food
          if($x('//div[@class="attack"]/p[@class="msg"]').length > 0 
          && $x('//div[@class="attack"]/p[@class="msg"]')[0].innerHTML != ""){
            log($x('//div[@class="attack"]/p[@class="msg"]')[0].innerHTML + "，跳转回首页重新开始所有流程");
            clickLink("/mindex");
            return;
          }
          //direct to submit the last step for attack
          var btn = $("#fbsb2").get(0);
          if($('#s2t').get(0) == null){
              log("当前狩猎规则存在错误,请进入军事指挥所纠正该错误.自动跳转回首页...");
              clickLink('/mindex');
              return;
          }
          $('#s2t').get(0).action='/s2t';
          //btn.disabled=true;
          var randomTime = Math.random()*15000;
          randomTime = randomTime<3000?3000:randomTime;
          window.setTimeout(function(){
            delete GM_safeVariables["ds_huntRuleCurrent"];
            $('#fbbtn').get(0).click();//auto transition to the start state,server will redirect the page to mindex page
          },randomTime);
          return;
        }
        //first phase ,selected the hero.
        var currentHuntRule = GM_safeVariables["ds_huntRuleCurrent"];
        currentHuntRule = eval('('+currentHuntRule+')');
        for(var i=0;i<currentHuntRule.heros.length;i++){
            var hero = currentHuntRule.heros[i];    
            $('#armys > li > h3:contains("'+hero.name+'")').siblings(':checkbox[name="heroid"]').get(0).checked = true;
        }
        var strHeroName = '';
        for(var m=0;m<currentHuntRule.heros.length;m++){
            var objHero = currentHuntRule.heros[m];
            strHeroName += objHero.name + ',';
        }
        if($(':checkbox:checked[name="heroid"]').length == 0){
            log("无法选中指定规则中包含的英雄'"+strHeroName+"',自动跳转到首页...");
            clickLink('/mindex');
            return;
        }
        var randomTime = Math.random()*15000;
        randomTime = randomTime<3000?3000:randomTime;
        window.setTimeout(function(){
            //transition to next state
            $x("//form[@action='/a2t']")[0].submit();
        },randomTime);
        return;
        log("没有可以出征的英雄，跳转到首页...");
        clickLink("/mindex");
    }else{
        log("您没有制定任何的狩猎规则,将自动跳转回首页...");
        clickLink('/mindex');
        return;
    }
}

function a2t_handler_configMode(){

}